package com.example.grpc.consume.service.impl;

import com.example.grpc.consume.service.HelloWorldService;
import com.example.grpc.generator.HelloRequest;
import com.example.grpc.generator.HelloResponse;
import com.example.grpc.generator.HelloServiceGrpc;
import net.devh.boot.grpc.client.inject.GrpcClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author liyuncong
 * @version 1.0
 * @file HelloWorldServiceImpl
 * @brief HelloWorldServiceImpl
 * @details HelloWorldServiceImpl
 * @date 2022-11-09
 *
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                        NAME               DESCRIPTION
 * 2022-11-09                  liyuncong          Created
 */
@Service
public class HelloWorldServiceImpl implements HelloWorldService {

    private static final Logger logger = LoggerFactory.getLogger(HelloWorldServiceImpl.class);

    //    @GrpcClient("local-grpc-server-1")
    @GrpcClient(value = "GLOBAL")
    private HelloServiceGrpc.HelloServiceBlockingStub helloServiceBlockingStub;

    @Override
    public String sayHello(String name) {
        if (!StringUtils.hasText(name)) {
            return null;
        }
        try {
            final HelloRequest request = HelloRequest
                .newBuilder()
                .setName("name")
                .setGender("gender")
                .build();
            final HelloResponse response = helloServiceBlockingStub.hello(request);
            return response.getGreeting();
        } catch (Exception exception) {
            logger.error("grpc request error ", exception);
        }
        return null;
    }
}
